import { getTemplateStatus} from "@/service/swagger/AcTemplateController";
import {getInstStatus} from '@/service/swagger/AcAppInstController'

import {tmpStatusFilter} from '@/filters/status/index';
import {appStatusFilter} from '@/filters/status/index';

import {listenner} from "@/utils/statuslistener" ;



// 中间状态处理列表的   参数(列表数据,vm对象,过滤函数名,监听状态函数名)
export function checkStatus(tableData,_this,filterFun,statusFun) {
  console.log("页面列表 search 完成", tableData);
  const statusStatic = (status) => {
    // 是否是稳定状态
    const isStatic = !(filterFun(status).indexOf("...") > -1);
    return isStatic;
  }
  let unStaticArr = [];
  // 添加  status 监听
  tableData.forEach(el => {
    if (!statusStatic(el.status)) {
      if(filterFun == tmpStatusFilter){
        unStaticArr.push(el.templateId)
      }else{
        unStaticArr.push(el.instId)
      }
    }
  });
  if (unStaticArr && unStaticArr.length) {
    listenner({
      api: {
        serviceFN: statusFun,
        params: unStaticArr,
      },
      callBack: (ret) => {

        let hasAllStatusStaticed = true;
        let hasUpdateing;
        ret.data.forEach(data => {
          if (!statusStatic(data.status)) {
            hasAllStatusStaticed = false;
          }
          tableData.forEach(el => {
            if(data.instId){
              if(data.instId == el.instId){
                // 如果是销毁中,部署中 成功后在刷一次列表
                if(data.status == 3 || data.status == 1){
                    el.status = data.status;
                    if(_this.$refs.listTpml){
                    	_this.$refs.listTpml.refresh()
                    }else {
                    	_this.$refs.listTable.search()
                    }
                }else{
                  el.status = data.status;
                }
              }
            }else{
              if(data.templateId == el.templateId){
                el.status = data.status;
                if(el.upgradeing==1){
                	hasUpdateing = true ;
                }
              }
            }
            if(data.hostsInfo){
            	el.hostsInfo = data.hostsInfo || [];
            }
          });
        });
				if(hasAllStatusStaticed&&hasUpdateing){
					_this.refresh()
				}else{
					if(_this.$refs.listTpml){
						_this.$refs.listTpml.loadTableData(tableData)
					}else if(_this.$refs.listTable){
						_this.$refs.listTable.serviceCallBack({
							data :tableData,
						},true)
					}
				}
        return hasAllStatusStaticed;
      },
    }, this)
  }
}

